#include <iostream>
using std::cout;
using std::endl;

class Point
{
public:
    static Point * getInstance(){
        if(_pInstance == nullptr){
            _pInstance = new Point(1, 2);
        }
        return _pInstance;
    }

    void init(int x, int y){
        _ix = x;
        _iy = y;
    }
    
    void print() const{
        cout << "(" << this->_ix
            << "," << this->_iy
            << ")" << endl;
    }

    static void destory(){ // 加上static后，可以直接通过类名调用成员函数
        if(_pInstance){
            delete _pInstance;
            _pInstance = nullptr;
        }
    }

private:
    Point() = default; // 显式定义有参构造后，默认有参构造就没有了，需要自己声明
    Point(const Point&) = delete;
    Point& operator=(const Point&) = delete;

    Point(int x = 0,int y = 0)
    : _ix(x)
    , _iy(y)
    {
        cout << "Point(int,int)" << endl;
    }

    ~Point(){
        cout << "~Point(int,int)" << endl;
    }    
private:
    int _ix;
    int _iy;
    static Point * _pInstance; // 保存第一次创建的对象
};

Point * Point::_pInstance = nullptr; // 初始化为空指针

void test1(){
    // 单例模式的规范使用方法 直接通过类调用函数
    Point::getInstance()->init(100, 200);
    Point::getInstance()->print();
    Point::getInstance()->init(10, 20);
    Point::getInstance()->print();

    // 即使多次调用，也不会出现double free的问题
    Point::destory();
};


int main(void){
    test1();
    return 0;
}

